// pages/show_label/show_label.js
const app = getApp(), core = require('../../utils/core'), util = require('../../utils/util');
  Page({

  /**
   * 页面的初始数据
   */
  data: {
    benefits: [
    ]
  },
  check:function(e){
    var index=e.currentTarget.dataset.index;
    this.data.benefits[index].check = !this.data.benefits[index].check;
    this.setData({benefits: this.data.benefits});
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getOpenerEventChannel().on('infoTransfer', data => {
      this.setData({
        info: data.info,
      });
      util.getBenefits().then(benefits=>{
        benefits.forEach((item)=>{
          for(let i=0;i<this.data.info.benefits.length;i++) {
            if(item.id==this.data.info.benefits[i].id) {
              item.check = true;
            }
          }
        });
        this.setData({benefits});
      });
    });
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  bindSubmit() {
    let benefits = [];
    this.data.benefits.forEach(item=>{
      if(item.check) {
        benefits.push(item);
      }
    });
    this.data.info.benefits = benefits;
    this.getOpenerEventChannel().emit('infoTransfer',{info:this.data.info});
    wx.navigateBack();
  }

});
